#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "AudioDecodeRaw.h"

#define READ_SIZE 4096

int main(int argc, char* argv[])
{
    //if(argc<2)
    //{
        //printf("usage:dec_mp3 infile\n");
        //return -1;
    //}
    //输入进行解码的mp3文件
    char input_file[256] = "test.mp3";

    //输出解码后获取到的pcm音频数据文件
    char output_file[256] = "outdec1.pcm";

    if(argc>1)
    {
        strcpy(input_file, argv[1]);
    }
    if(argc>2)
    {
        strcpy(output_file, argv[2]);
    }

start:
    AudioDecodeRaw dec;
    //dec.init("aac");
    //dec.init(AV_CODEC_ID_AAC);
    dec.init(AV_CODEC_ID_OPUS);


    FILE *fin, *fout;

    fin = fopen(input_file, "rb");
    if (!fin) {
        fprintf(stderr, "Could not open %s\n", input_file);
        exit(1);
    }
    printf("open input_file %s ok\n", input_file);
    fout = fopen(output_file, "wb");
    if (!fout) {
        exit(1);
    }
    printf("open output_file %s ok\n", output_file);

    while(!feof(fin))
    {
        uint8_t in_buffer[READ_SIZE];
        int in_len = fread(in_buffer, 1, READ_SIZE, fin);
        //printf("in size = %d\n", len);
        uint8_t* out_buffer;
        int out_len = dec.decode((const uint8_t*)in_buffer, in_len, &out_buffer);
        if (out_len < 0) {
            fprintf(stderr, "Error while parsing\n");
            exit(1);
        }
        //printf("out size = %d\n", ret);
        fwrite(out_buffer, 1, out_len, fout);
    }

    fclose(fout);
    fclose(fin);

    //goto start;

    return 0;
}

